<?php

namespace App\Admin\Forms;

use App\Models\Posts\WxPost;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Cache;

class PostMetaForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $id = $input['id'];
        if(_empty_($id)){
            return $this->response()->warning('参数错误');
        }

        $title = trim(strip_tags($input['title']));

        if(!_empty_($title)){
            WxPost::withoutSyncingToSearch(function () use ($id, $title){
                WxPost::where('id', $id)->update([
                    'posts_title' => $title
                ]);
            });
        }

        return $this->response()
            ->success('操作成功.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">提示: 一般情况下，你只需在编辑器换行，系统就会识别到标题，若是特殊标题，如字数过长，可在这配置。<br/><br/>优缺点：系统识别的标题可以展现表情，艾特等内容，但这里配置的不会展现</div>';
        }, '');
        $this->hidden('id')->value($this->payload["id"]);
        $this->textarea('title', '标题')->help('自定义标题只有图文起作用')->default(WxPost::where('id', $this->payload["id"])->value('posts_title'));
    }

}
